package be.tf.freemaind.main;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import be.tf.freemaind.dao.PostDAO;
import be.tf.freemaind.dao.UserDao;
import be.tf.freemaind.model.Post;
import be.tf.freemaind.model.User;

public class JDBCMain {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/freemaind", "freemaind", "freemaind");
			Statement stm = connexion.createStatement();
			
			String string;
			UserDao userDao = new UserDao((com.mysql.jdbc.Statement) stm);
			
			User user = userDao.findByUserName("root");
			
			System.out.println("user found: "+user.getName()+" pwd "+user.getPassword());
			System.out.println("logged? "+userDao.login(user));
			System.out.println("role? "+user.getRole());
			
			List<Post> posts = new PostDAO((com.mysql.jdbc.Statement) stm).findAll();
			
			for(Post post:posts){
				post.getIdpost();
			}
			
//			ResultSet resultat = statement.executeQuery("select * from user;");
//			
//			while (resultat.next()) {
//				System.out.println("ID : " + resultat.getInt("iduser") 
//							+ " *** nom : " + resultat.getString("name")
//							+ " *** password : " + resultat.getString("password")
//							+ " *** email : " + resultat.getString("email")
//							+ " *** role : " + resultat.getString("role")
//							);
//			}
//			
//			resultat.close();
//			statement.close();
//			connexion.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.print("Impossible de se connect� � MySQL");
		}
	}

	private static PrintWriter PrintWriter() {
		// TODO Auto-generated method stub
		return null;
	}
}
